package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Author;
import com.ruoyi.system.domain.Category;
import com.ruoyi.system.domain.Novel;
import com.ruoyi.system.domain.Vo.NovelVo;
import com.ruoyi.system.service.IAuthorService;
import com.ruoyi.system.service.ICategoryService;
import com.ruoyi.system.service.INovelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 小说Controller
 *
 * @author 许雅萍
 * @date 2024-11-16
 */
@Controller
@RequestMapping("/system/novel")
public class NovelController extends BaseController {
    private String prefix = "system/novel";

    @Autowired
    private INovelService novelService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IAuthorService authorService;


    @GetMapping()
    public String novel() {
        return prefix + "/novel";
    }

    /**
     * 查询小说列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Novel novel) {
        // PageHelper 分页
        startPage();
        List<Novel> list = novelService.selectNovelList(novel);

        // 获取 PageHelper 的分页信息
        PageInfo<Novel> pageInfo = new PageInfo<>(list);

        List<NovelVo> novelVos = new ArrayList<>();
        list.forEach(n -> {
            Long categoryId = n.getCategoryId();
            Long authorId = n.getAuthorId();

            // 清除分页参数，避免干扰
            PageHelper.clearPage();
            Category category = categoryService.selectCategoryById(categoryId);
            Author author = authorService.selectAuthorByAuthorId(authorId);

            NovelVo novelVo = new NovelVo();
            BeanUtils.copyProperties(n, novelVo);
            novelVo.setAuthorName(author.getName());
            novelVo.setCategoryName(category.getName());
            novelVos.add(novelVo);
        });

        // 使用 PageHelper 的分页数据构建响应
        TableDataInfo dataInfo = new TableDataInfo();
        dataInfo.setCode(0);
        dataInfo.setRows(novelVos);
        dataInfo.setTotal(pageInfo.getTotal());  // 正确获取总记录数

        return dataInfo;
    }

    /**
     * 导出小说列表
     */

    @Log(title = "小说", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Novel novel) {
        List<Novel> list = novelService.selectNovelList(novel);
        ExcelUtil<Novel> util = new ExcelUtil<Novel>(Novel.class);
        return util.exportExcel(list, "小说数据");
    }

    /**
     * 新增小说
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        Category category = new Category();
        category.setStatus(0);
        List<Category> categories = categoryService.selectCategoryList(category);
        modelMap.put("categoryList", categories);

        List<Author> authors = authorService.selectAuthorList(new Author());
        modelMap.put("authors", authors);
        return prefix + "/add";
    }

    /**
     * 新增保存小说
     */

    @Log(title = "小说", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Novel novel) {
        return toAjax(novelService.insertNovel(novel));
    }

    /**
     * 修改小说
     */

    @GetMapping("/edit/{novelId}")
    public String edit(@PathVariable("novelId") Long novelId, ModelMap mmap) {
        Novel novel = novelService.selectNovelByNovelId(novelId);
        mmap.put("novel", novel);

        Category category = new Category();
        category.setStatus(0);
        List<Category> categories = categoryService.selectCategoryList(category);
        mmap.put("categoryList", categories);

        List<Author> authors = authorService.selectAuthorList(new Author());
        mmap.put("authors", authors);
        return prefix + "/edit";
    }

    /**
     * 修改保存小说
     */

    @Log(title = "小说", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Novel novel) {
        return toAjax(novelService.updateNovel(novel));
    }

    /**
     * 删除小说
     */

    @Log(title = "小说", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(novelService.deleteNovelByNovelIds(ids));
    }
}
